/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package negocio.dao;

import java.sql.SQLException;
import java.util.List;
import negocio.Alquiler;
import negocio.HibernateUtil;
import negocio.*;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Javier
 */
public class DAO {
    
    
    Session hSession = null;

    public DAO() {
        this.hSession = HibernateUtil.getSessionFactory().openSession();
    }
    
    public List<Object> list(String tabla)throws SQLException {
        hSession.clear();
        Query q = hSession.createQuery("from "+tabla);
        List resultados = (List<Object>) q.list();
        return resultados;
    }
    
     public Object read(String tabla, Integer id)throws SQLException {
        hSession.clear();
        Query q = hSession.createQuery("from "+tabla+" where id"+tabla+"='"+id+"'");
        Object u =  q.uniqueResult();
        return u;
    }
     
    public int delete(String tabla, Integer id) throws SQLException {
        hSession.clear();
        String hql = "delete from "+tabla+" where id"+tabla+"='"+id+"'";
        Query query = hSession.createQuery(hql);
        hSession.beginTransaction();
        int rowCount = query.executeUpdate();
        hSession.getTransaction().commit();
        return rowCount;
    } 
    
    public void create(Object o, String tipo) throws SQLException {
         hSession.beginTransaction();
        switch (tipo){
            case "Alquiler":
                hSession.save((Alquiler)o);
                break;
            case "Horario":
                hSession.save((Horario)o);
                break;
           case "HorarioPista":
                hSession.save((Horariopista)o);
                break;
           case "Instalacion":
                hSession.save((Instalacion)o);
                break;
           case "Pista":
                hSession.save((Pista)o);
                break;   
           case "Usuario":
                hSession.save((Usuario)o);
                break; 
           case "Valoracion":
                hSession.save((Valoracion)o);
                break;   
        }
        hSession.getTransaction().commit();
    }
    
    public void update(Object o, String tipo) throws SQLException {
         hSession.beginTransaction();
        switch (tipo){
            case "Alquiler":
                hSession.update((Alquiler)o);
                break;
            case "Horario":
                hSession.update((Horario)o);
                break;
           case "HorarioPista":
                hSession.update((Horariopista)o);
                break;
           case "Instalacion":
                hSession.update((Instalacion)o);
                break;
           case "Pista":
                hSession.update((Pista)o);
                break;   
           case "Usuario":
                hSession.update((Usuario)o);
                break; 
           case "Valoracion":
                hSession.update((Valoracion)o);
                break;   
        }
        hSession.getTransaction().commit();
    }
    
    
    
}
